import os


def wordOpt(base):
    os.system("clear")

    def showMethod():
        print("a.添加单词表")
        print("b.删除单词表")
        print("c.修改单词表")
        print("d.添加单词")
        print("e.删除单词")
        print("f.修改单词")
        print("g.返回上一级")

    def showAllTables():
        allTables = base.selectAllTable()
        allTables.remove(('sqlite_sequence',))
        if not len(allTables):
            print("警告：没有单词表，请先添加")
            return False
        print("提示：以下为所有的单词表")
        for i in allTables:
            print("{}.{}".format(str(allTables.index(i) + 1), i[0]))
        return allTables

    showMethod()
    while True:
        ipt = input("请输入序号：")
        if ipt == "a":
            iptTableName = input("请输入单词表的名称：")
            if iptTableName != 'exit':
                try:
                    base.createTable(iptTableName, '''
                        "id"	INTEGER PRIMARY KEY AUTOINCREMENT,
                        "english"	TEXT,
                        "chinese"	TEXT,
                        "group"    TEXT
                        ''')
                    print("提示：单词表%s创建成功" % iptTableName)
                except Exception as e:
                    print(e)
                    print("错误：此操作出错，请重试")
            else:
                os.system("clear")
                showMethod()

        elif ipt == 'b':
            allTables = showAllTables()
            if (not allTables):
                continue
            iptDelTable = input("请输入单词表的序号进行删除：")
            if iptDelTable != "exit":
                try:
                    base.deleteTable(allTables[int(iptDelTable) - 1][0])
                    print("提示：单词表%s删除成功" % allTables[int(iptDelTable) - 1][0])
                except Exception as e:
                    # print(e)
                    print("错误：此操作出错，请重试")
            else:
                os.system("clear")
                showMethod()

        elif ipt == 'c':
            allTables = showAllTables()
            if (not allTables):
                continue
            iptPassTable = input("请输入要更改的单词表序号：")
            if iptPassTable != 'exit':
                iptNewTable = input("请输入修改后的单词表名称：")
                if iptNewTable != 'exit':
                    try:
                        base.updateTbName(allTables[int(iptPassTable) - 1][0], iptNewTable)
                    except:
                        print("错误：此操作出错，请重试")
            else:
                os.system("clear")
                showMethod()

        elif ipt == 'd':
            allTables = showAllTables()
            if (not allTables):
                continue
            iptTable = input("请输入单词所在的单词表：")
            if iptTable != 'exit':
                iptGroup = input("请输入单词所在的组(自己查!!!无则创建)：")
                if iptGroup != 'exit':
                    print(r"提示：如果添加单个单词，添加完输入'exit'即可退出")
                    try:
                        print(r"提示：如果添加连续的单词，则这组单词将一直在'{}'->'{}'中".format(allTables[int(iptTable) - 1][0], iptGroup))
                    except:
                        print("错误：此操作出错，请重试")
                        continue
                    print("提示：要修改表和组，退出重新选择即可")
                    while True:
                        iptEnglish = input("English：")
                        if iptEnglish != 'exit':
                            iptChinese = input("Chinese：")
                            if iptChinese != 'exit':
                                if iptEnglish.strip() != '' or iptChinese.strip() != '':
                                    try:
                                        insert_to = 'INSERT INTO {0} VALUES ({1})'.format(
                                            allTables[int(iptTable) - 1][0],
                                            '{}, "{}", "{}", "{}"'.format('NULL', iptEnglish, iptChinese, iptGroup))
                                        base.cur.execute(insert_to)
                                        base.conn.commit()
                                    except Exception as e:
                                        # print(e)
                                        print("错误：此操作出错，请重试")
                                else:
                                    print("警告：两者有一个不能为空")
                            else:
                                break
                        else:
                            break
            os.system("clear")
            showMethod()

        elif ipt == 'e':
            allTables = showAllTables()
            if (not allTables):
                continue
            iptTableNum = input("请输入删除的单词所在的单词表的序号：")
            try:
                iptTable = allTables[int(iptTableNum) - 1][0]
            except:
                print("错误：此操作出错，请重试")
            else:
                if iptTable != 'exit':
                    os.system("clear")
                    while True:
                        print("1.删除一组的单词")
                        print("2.删除单个单词")
                        print("3.返回上一级")
                        iptDelWord = input("请选择删除方式的序号：")
                        if iptDelWord == '3':
                            break
                        elif iptDelWord == '1':
                            iptDelGpNe = input("请输入删除的组名(自己查!!!)：")
                            if iptDelGpNe == 'exit':
                                continue
                            try:
                                base.deleteFrom(iptTable, '"group"="%s"' % iptDelGpNe)
                                wordsId = [i[0] for i in base.findAll(iptTable)]
                                wordsLen = len(wordsId)
                                for i in wordsId:
                                    if i != wordsId.index(i) + 1:
                                        base.updateTo(iptTable, "id=%s" % str(i), "id=%s" % str(wordsId.index(i) + 1))
                                base.updateTo('sqlite_sequence', 'name="%s"' % iptTable, 'seq=%s' % str(wordsLen))
                            except Exception as e:
                                # print(e)
                                os.system("clear")
                                print("错误：此操作出错，请重试")
                                continue
                            else:
                                os.system("clear")
                                print(r"提示'{}'->'{}'删除成功".format(iptTable, iptDelGpNe))
                        elif iptDelWord == '2':
                            iptDelWdId = input("请输入删除的单词的ID(自己查!!!)：")
                            if iptDelWdId == 'exit':
                                os.system("clear")
                                continue
                            try:
                                base.deleteFrom(iptTable, 'id=%s' % iptDelWdId)
                                wordsId = [i[0] for i in base.findAll(iptTable)]
                                wordsLen = len(wordsId)
                                for i in wordsId:
                                    if i != wordsId.index(i) + 1:
                                        base.updateTo(iptTable, "id=%s" % str(i), "id=%s" % str(wordsId.index(i) + 1))
                                base.updateTo('sqlite_sequence', 'name="%s"' % iptTable, 'seq=%s' % str(wordsLen))
                            except:
                                print("错误：此操作出错，请重试")
                                os.system("clear")
                                continue
                            else:
                                os.system("clear")
                                print(r"提示'{}'->'{}'删除成功".format(iptTable, iptDelWdId))

                    os.system("clear")
                    showMethod()

        elif ipt == 'f':
            allTables = showAllTables()
            if (not allTables):
                continue
            iptTableNum = input("请输入修改的单词所在的单词表的序号：")
            if iptTableNum == 'exit':
                os.system("clear")
                showMethod()
                continue
            try:
                iptTable = allTables[int(iptTableNum) - 1][0]
            except:
                print("错误：此操作出错，请重试")
            else:
                iptEdId = input("请输入修改的单词的ID(自己查!!!)：")
                if iptEdId == 'exit':
                    os.system("clear")
                    showMethod()
                    continue
                base.cur.execute('select * from {} where id = {};'.format(iptTable, iptEdId))
                getWord = base.cur.fetchall()
                if len(getWord) == 0:
                    os.system("clear")
                    print("警告：没有找到此单词请先添加")
                    showMethod()
                    continue
                edChinese = getWord[0][2]
                edEnglish = getWord[0][1]
                edGroup = getWord[0][3]
                print("{}:{}:{}:{}".format(iptTable, edGroup, edEnglish, edChinese))
                print("1.修改中文")
                print("2.修改英文")
                print("3.修改组别")
                iptEdWay = input("请输入修改内容的序号：")
                if iptEdWay == 'exit':
                    os.system("clear")
                    showMethod()
                    continue
                elif iptEdWay == '1':
                    edpChinese = input("请输入修改后的中文：")
                    try:
                        base.updateTo(iptTable, "id=%s" % iptEdId, 'chinese="%s"' % edpChinese)
                    except:
                        os.system("clear")
                        print("错误：此操作出错，请重试")
                        showMethod()
                    else:
                        os.system("clear")
                        print(r"提示'{}:{}:{}:{}'修改成功".format(iptTable, edGroup, edEnglish, edpChinese))
                        showMethod()

                elif iptEdWay == '2':
                    edpEnglish = input("请输入修改后的英文：")
                    try:
                        base.updateTo(iptTable, "id=%s" % iptEdId, 'english="%s"' % edpEnglish)
                    except:
                        os.system("clear")
                        print("错误：此操作出错，请重试")
                        showMethod()
                    else:
                        os.system("clear")
                        print(r"提示'{}:{}:{}:{}'修改成功".format(iptTable, edGroup, edpEnglish, edChinese))
                        showMethod()
                elif iptEdWay == '3':
                    edpGroup = input("请输入修改后的组别：")
                    try:
                        base.updateTo(iptTable, "id=%s" % iptEdId, 'group="%s"' % edpGroup)
                    except:
                        os.system("clear")
                        print("错误：此操作出错，请重试")
                        showMethod()
                    else:
                        os.system("clear")
                        print(r"提示'{}:{}:{}:{}'修改成功".format(iptTable, edpGroup, edEnglish, edChinese))
                        showMethod()
                else:
                    os.system("clear")
                    print("错误：输入有误，请重试")
                    showMethod()



        elif ipt == 'g' or ipt == 'exit':
            break

        else:
            print('错误：没有此方法')
